#include "StdAfx.h"
#include "GeomApp.h"
#include "AnyApplication.h"
#include "ObjectFactory.h"
#include "PropFactory.h"
#include "IAnyScript.h"
#include "EditorConfig.h"
#include "PlugsConfigure.h"

CGeomApp::CGeomApp(void)
{
	m_pObjectTree = NULL;
	m_pPlugs = new CPlugManager;
	m_pScript = NULL;
}

CGeomApp::~CGeomApp(void)
{
	delete m_pPlugs;
}

BOOL CGeomApp::Initialize()
{
	//InitCommandMap();

	if(!GetAnyApp().Initialize())
		return false;

	GetPropFactory().Initialize();

	GetEditorConfig().Load(_T("./Config.xml"));

	m_pPlugs->LoadPlug(_T("Script.dll"));
	if(m_pScript)
		m_pScript->Register();

	m_pPlugs->LoadPlug(_T("Primitive.dll"));

	m_pPlugs->LoadPlug(_T("Exporter.dll"));

	GetCPlugsConfigure().Load(_T("./Plugs.xml"));
	return true;
}

void CGeomApp::SetPropScript(IPropScript* pScript)
{
	m_pScript = pScript;
}

bool CGeomApp::LoadEditorProps(const char *szScrip, 
							   CPropWrapper* pPropWrapper)
{
	if(m_pScript && pPropWrapper)
		return m_pScript->InitPropList(szScrip, pPropWrapper);

	return false;
}

CView* GetActiveView()
{
	CMDIFrameWnd* pMainFrm = (CMDIFrameWnd*)AfxGetMainWnd();
	ASSERT(pMainFrm);
	if(NULL == pMainFrm)
		return NULL;

	CFrameWnd* pChildFrm = pMainFrm->GetActiveFrame();
	if(pChildFrm)
	{
		return pChildFrm->GetActiveView();
	}

	return NULL;
}

void SendMessageToActiveView(UINT nMsg, WPARAM wp, LPARAM lp)
{
	CView* pView = GetActiveView();
	if(pView)
	{
		::SendMessage(pView->GetSafeHwnd(), nMsg, wp, lp);
	}
}